#include "pch.h"
#include "resource.h"
#include "CViewportController.h"
#include "CBandList.h"

CViewportController::CViewportController(ImageBands bands, BOOL ABFlag)
	: mainDlg(nullptr), zoomDlg(nullptr), scrollDlg(nullptr), mainDlgA(nullptr), zoomDlgA(nullptr), scrollDlgA(nullptr), m_Bands(bands)
{
	bRmin = bGmin = bBmin = 0;
	bRmax = bGmax = bBmax = 255;
	iPercent = 0;

	m_ABFlagIsA = ABFlag;
	BaseBands = bands;
	if (ABFlag == 0)
	{
		mainDlg = new CBase1Dlg();
		mainDlg->m_pViewController = this;
		mainDlg->Create(IDD_DIALOG_BASE_1);
		mainDlg->ShowWindow(SW_SHOW);

		zoomDlg = new CBase2Dlg();
		zoomDlg->m_pViewController = this;
		zoomDlg->Create(IDD_DIALOG_BASE_2);
		zoomDlg->ShowWindow(SW_SHOW);

		scrollDlg = new CBase3Dlg();
		scrollDlg->m_pViewController = this;
		scrollDlg->Create(IDD_DIALOG_BASE_3);
		scrollDlg->ShowWindow(SW_SHOW);

		mainDlg->GetClientRect(&mainrect);
		zoomDlg->GetClientRect(&zoomrect);
		scrollDlg->GetClientRect(&scrollrect);

		mainpos.x = int(float(zoomrect.right - zoomrect.left) / 3) / 2;
		mainpos.y = int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;

		zoompos.x = (zoomrect.right - zoomrect.left) / 2;
		zoompos.y = (zoomrect.bottom - zoomrect.top) / 2;

		maindisplaypos.x = maindisplaypos.y = 0;
		zoomdisplaypos.x = zoomdisplaypos.y = 0;

		MousePos.x = zoomdisplaypos.x + zoompos.x / 3;
		MousePos.y = zoomdisplaypos.y + zoompos.y / 3;

		if (bands.iHeight > bands.iWidth)
		{
			iNewHeight = bands.iHeight;
			iNewWidth = (int)((float)bands.iHeight / (mainrect.bottom - mainrect.top) * (mainrect.right - mainrect.left));
		}
		else
		{
			iNewWidth = bands.iWidth;
			iNewHeight = (int)((float)bands.iWidth / (mainrect.right - mainrect.left) * (mainrect.bottom - mainrect.top));
		}

		scrollpos.x = int(float(mainrect.right - mainrect.left) / iNewWidth * (scrollrect.right - scrollrect.left)) / 2;
		scrollpos.y = int(float(mainrect.bottom - mainrect.top) / iNewHeight * (scrollrect.bottom - scrollrect.top)) / 2;

		DrawBaseImage();
		//DrawControlPoint();
	}
	else if (ABFlag == 1)
	{
		mainDlgA = new CAjust1Dlg();
		mainDlgA->m_pViewController = this;
		mainDlgA->Create(IDD_DIALOG_AJUST_1);
		mainDlgA->ShowWindow(SW_SHOW);

		zoomDlgA = new CAjust2Dlg();
		zoomDlgA->m_pViewController = this;
		zoomDlgA->Create(IDD_DIALOG_AJUST_2);
		zoomDlgA->ShowWindow(SW_SHOW);

		scrollDlgA = new CAjust3Dlg();
		scrollDlgA->m_pViewController = this;
		scrollDlgA->Create(IDD_DIALOG_AJUST_3);
		scrollDlgA->ShowWindow(SW_SHOW);

		mainDlgA->GetClientRect(&mainrect);
		zoomDlgA->GetClientRect(&zoomrect);
		scrollDlgA->GetClientRect(&scrollrect);

		mainpos.x = int(float(zoomrect.right - zoomrect.left) / 3) / 2;
		mainpos.y = int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;

		zoompos.x = (zoomrect.right - zoomrect.left) / 2;
		zoompos.y = (zoomrect.bottom - zoomrect.top) / 2;

		maindisplaypos.x = maindisplaypos.y = 0;
		zoomdisplaypos.x = zoomdisplaypos.y = 0;

		MousePos.x = zoomdisplaypos.x + zoompos.x / 3;
		MousePos.y = zoomdisplaypos.y + zoompos.y / 3;

		if (bands.iHeight > bands.iWidth)
		{
			iNewHeight = bands.iHeight;
			iNewWidth = (int)((float)bands.iHeight / (mainrect.bottom - mainrect.top) * (mainrect.right - mainrect.left));
		}
		else
		{
			iNewWidth = bands.iWidth;
			iNewHeight = (int)((float)bands.iWidth / (mainrect.right - mainrect.left) * (mainrect.bottom - mainrect.top));
		}

		scrollpos.x = int(float(mainrect.right - mainrect.left) / iNewWidth * (scrollrect.right - scrollrect.left)) / 2;
		scrollpos.y = int(float(mainrect.bottom - mainrect.top) / iNewHeight * (scrollrect.bottom - scrollrect.top)) / 2;

		DrawBaseImage();
		//DrawControlPoint();
	}
}

void CViewportController::ResetWindowsPos()
{
	if (m_ABFlagIsA == 0)
	{
		CRect rectMainDlg;
		mainDlg->GetWindowRect(rectMainDlg);

		zoomDlg->MoveWindow(rectMainDlg.left, rectMainDlg.bottom, 300, 300);
		scrollDlg->MoveWindow(rectMainDlg.left + 300, rectMainDlg.bottom, 300, 300);
	}
	else if (m_ABFlagIsA == 1)
	{
		CRect rectMainDlg;
		mainDlgA->GetWindowRect(rectMainDlg);

		zoomDlgA->MoveWindow(rectMainDlg.left, rectMainDlg.bottom, 300, 300);
		scrollDlgA->MoveWindow(rectMainDlg.left + 300, rectMainDlg.bottom, 300, 300);
	}
}

void CViewportController::SetFocus()
{
	if (mainDlg)
	{
		mainDlg->SetFocus();
	}
}

void CViewportController::LinearStretch(int iPercent)
{
	int FactR[256] = { 0 };
	int FactG[256] = { 0 };
	int FactB[256] = { 0 };
	int iAllCounta = 0;
	for (int ii = 0; ii < BaseBands.iHeight; ii++)
	{
		BYTE * TempR = BaseBands.pBandR[ii];
		BYTE * TempG = BaseBands.pBandG[ii];
		BYTE * TempB = BaseBands.pBandB[ii];
		for (int jj = 0; jj < BaseBands.iWidth; jj++)
		{
			FactR[TempR[jj]]++;
			FactG[TempG[jj]]++;
			FactB[TempB[jj]]++;
			iAllCounta++;
		}
		
	}
	int iAllCount = 0;
	for (int ii = 0; ii < 256; ii++)
	{
		iAllCount = iAllCount + FactR[ii];
	}
	LinearStretch(iPercent, FactR, bRmax, bRmin);
	LinearStretch(iPercent, FactG, bGmax, bGmin);
	LinearStretch(iPercent, FactB, bBmax, bBmin);
}

void CViewportController::LinearStretch(int iPercent, int * Fact, BYTE& max, BYTE& min)
{
	max = 255;
	min = 0;

	int iAllCount = 0;
	for (int ii = 0; ii < 256; ii++)
	{
		iAllCount = iAllCount + Fact[ii];
	}

	int iTempCount = 0;
	for (int ii = 1; ii < 256; ii++)
	{
		iTempCount = iTempCount + Fact[ii];
		float a = iTempCount / (float)iAllCount * 100;
		if (iTempCount / (float)iAllCount * 100 < iPercent)
			min++;
		else
		{
			iTempCount = 0;
			break;
		}	
	}
	for (int ii = 255; ii >= 0; ii--)
	{
		iTempCount = iTempCount + Fact[ii];
		if (iTempCount / (float)iAllCount * 100 < iPercent)
			max--;
		else
			break;
	}
}

void CViewportController::DrawBaseImage()
{
	CDC * pmainDC = NULL;
	CDC * pzoomDC = NULL;
	CDC * pscrollDC = NULL;
	if (m_ABFlagIsA == 0)
	{
		pmainDC = mainDlg->GetDC();
		pzoomDC = zoomDlg->GetDC();
		pscrollDC = scrollDlg->GetDC();
	}
	else if (m_ABFlagIsA == 1)
	{
		pmainDC = mainDlgA->GetDC();
		pzoomDC = zoomDlgA->GetDC();
		pscrollDC = scrollDlgA->GetDC();
	}
	

	DrawMainBaseimage(BaseBands, pmainDC, mainrect, mainpos);
	DrawZoomBaseimage(BaseBands, pzoomDC, zoomrect, zoompos);
	DrawScrollBaseimage(BaseBands, pscrollDC, scrollrect, scrollpos);

	if (m_ABFlagIsA == 0)
	{
		 mainDlg->ReleaseDC(pmainDC);
		 zoomDlg->ReleaseDC(pzoomDC);
		 scrollDlg->ReleaseDC(pscrollDC);
	}
	else if (m_ABFlagIsA == 1)
	{
		 mainDlgA->ReleaseDC(pmainDC);
		 zoomDlgA->ReleaseDC(pzoomDC);
		 scrollDlgA->ReleaseDC(pscrollDC);
	}
}

void CViewportController::DrawMainBaseimage(ImageBands bands, CDC * pDC, CRect rect, POINT pos)
{
	CRect SelectArea;
	SelectArea.left = pos.x - int(float(zoomrect.right - zoomrect.left) / 3) / 2;
	SelectArea.top = pos.y - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;
	SelectArea.right = pos.x + int(float(zoomrect.right - zoomrect.left) / 3) / 2;
	SelectArea.bottom = pos.y + int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;

	CImage img;
	img.CreateEx(rect.right - rect.left, rect.bottom - rect.top, 24, BI_RGB, NULL, 0);
	LPBYTE pbData = (LPBYTE)img.GetBits();
	INT nPitch = img.GetPitch();// 
	for (int ii = maindisplaypos.y; ii < maindisplaypos.y + rect.bottom - rect.top; ii++)
	{
		UCHAR * TempR = bands.pBandR[ii];
		UCHAR * TempG = bands.pBandG[ii];
		UCHAR * TempB = bands.pBandB[ii];
		LPBYTE pbLineData = pbData + (ii - maindisplaypos.y) * nPitch;
		for (int jj = maindisplaypos.x; jj < maindisplaypos.x + rect.right - rect.left; jj++)
		{
			pbLineData[(jj - maindisplaypos.x) * 3] = AdjustData(TempB[jj], bBmin, bBmax);	// B
			pbLineData[(jj - maindisplaypos.x) * 3 + 1] = AdjustData(TempG[jj], bGmin, bGmax);	// G
			pbLineData[(jj - maindisplaypos.x) * 3 + 2] = AdjustData(TempR[jj], bRmin, bRmax);	// R
		}
	}

	for (int ii = 0; ii < rect.bottom - rect.top; ii++)
	{
		LPBYTE pbLineData = pbData + ii * nPitch;
		for (int jj = 0; jj < rect.right - rect.left; jj++)
		{
			if (ii == SelectArea.top || ii == SelectArea.bottom)
			{
				if (jj >= SelectArea.left && jj <= SelectArea.right)
				{
					pbLineData[jj * 3] = 0;	// B
					pbLineData[jj * 3 + 1] = 0;	// G
					pbLineData[jj * 3 + 2] = 255;	// R
				}
			}
			if (jj == SelectArea.left || jj == SelectArea.right)
			{
				if (ii >= SelectArea.top && ii <= SelectArea.bottom)
				{
					pbLineData[jj * 3] = 0;	// B
					pbLineData[jj * 3 + 1] = 0;	// G
					pbLineData[jj * 3 + 2] = 255;	// R
				}
			}
		}
	}

	img.Draw(pDC->m_hDC, rect);
}

void CViewportController::DrawZoomBaseimage(ImageBands bands, CDC * pDC, CRect rect, POINT pos)
{
	CImage img;
	img.CreateEx(rect.right - rect.left, rect.bottom - rect.top, 24, BI_RGB, NULL, 0);
	LPBYTE pbData = (LPBYTE)img.GetBits();
	INT nPitch = img.GetPitch();// 
	for (int ii = zoomdisplaypos.y; ii < zoomdisplaypos.y + rect.bottom - rect.top; ii++)
	{
		UCHAR * TempR = bands.pBandR[zoomdisplaypos.y + (ii - zoomdisplaypos.y) / 3];
		UCHAR * TempG = bands.pBandG[zoomdisplaypos.y + (ii - zoomdisplaypos.y) / 3];
		UCHAR * TempB = bands.pBandB[zoomdisplaypos.y + (ii - zoomdisplaypos.y) / 3];
		LPBYTE pbLineData = pbData + (ii - zoomdisplaypos.y) * nPitch;
		for (int jj = zoomdisplaypos.x; jj < (zoomdisplaypos.x + rect.right - rect.left); jj++)
		{
 			pbLineData[(jj - zoomdisplaypos.x) * 3] = AdjustData(TempB[zoomdisplaypos.x + (jj - zoomdisplaypos.x) / 3], bBmin, bBmax);	// B
			pbLineData[(jj - zoomdisplaypos.x) * 3 + 1] = AdjustData(TempG[zoomdisplaypos.x + (jj - zoomdisplaypos.x) / 3], bGmin, bGmax);	// G
			pbLineData[(jj - zoomdisplaypos.x) * 3 + 2] = AdjustData(TempR[zoomdisplaypos.x + (jj - zoomdisplaypos.x) / 3], bRmin, bRmax);	// R
		}
	}

	for (int ii = 0; ii < 0 + rect.bottom - rect.top; ii++)
	{
		LPBYTE pbLineData = pbData + ii * nPitch;
		for (int jj = 0; jj < (0 + rect.right - rect.left); jj++)
		{
			if (ii == pos.y || jj == pos.x)
			{
				pbLineData[jj * 3] = 0;	// B
				pbLineData[jj * 3 + 1] = 0;	// G
				pbLineData[jj * 3 + 2] = 255;	// R
			}
		}
	}

	img.Draw(pDC->m_hDC, rect);
}

void CViewportController::DrawScrollBaseimage(ImageBands bands, CDC * pDC, CRect rect, POINT pos)
{
	CRect SelectArea;
	SelectArea.left = pos.x - int(float(mainrect.right - mainrect.left) / iNewWidth * (rect.right - rect.left)) / 2;
	SelectArea.top = pos.y - int(float(mainrect.bottom - mainrect.top) / iNewWidth * (rect.right - rect.left)) / 2;
	SelectArea.right = pos.x + int(float(mainrect.right - mainrect.left) / iNewWidth * (rect.right - rect.left)) / 2;
	SelectArea.bottom = pos.y + int(float(mainrect.bottom - mainrect.top) / iNewWidth * (rect.right - rect.left)) / 2;

	CImage img;
	img.CreateEx(rect.right - rect.left, rect.bottom - rect.top, 24, BI_RGB, NULL, 0);
	LPBYTE pbData = (LPBYTE)img.GetBits();
	INT nPitch = img.GetPitch();// 
	if (bands.iWidth > bands.iHeight)
	{
		for (int ii = 0; ii < int((float(rect.bottom - rect.top)) / iNewHeight * bands.iHeight); ii++)
		{
			UCHAR * TempR = bands.pBandR[int(ii / float(rect.bottom - rect.top) * bands.iHeight)];
			UCHAR * TempG = bands.pBandG[int(ii / float(rect.bottom - rect.top) * bands.iHeight)];
			UCHAR * TempB = bands.pBandB[int(ii / float(rect.bottom - rect.top) * bands.iHeight)];
			LPBYTE pbLineData = pbData + ii * nPitch;
			for (int jj = 0; jj < rect.right - rect.left; jj++)
			{
				pbLineData[jj * 3] = AdjustData(TempB[int(jj / float(rect.right - rect.left) * bands.iWidth)], bBmin, bBmax); 
				pbLineData[jj * 3 + 1] = AdjustData(TempG[int(jj / float(rect.right - rect.left) * bands.iWidth)], bGmin, bGmax);	// G
				pbLineData[jj * 3 + 2] = AdjustData(TempR[int(jj / float(rect.right - rect.left) * bands.iWidth)], bRmin, bRmax);	// R
			}
		}
		for (int ii = int((float(rect.bottom - rect.top)) / iNewHeight * bands.iHeight); ii < rect.bottom - rect.top; ii++)
		{
			LPBYTE pbLineData = pbData + ii * nPitch;
			for (int jj = 0; jj < rect.right - rect.left; jj++)
			{
				pbLineData[jj * 3] = 255;	// B
				pbLineData[jj * 3 + 1] = 255;	// G
				pbLineData[jj * 3 + 2] = 255;	// R
			}
		}
	}
	else
	{
		for (int ii = 0; ii < rect.bottom - rect.top; ii++)
		{
			UCHAR * TempR = bands.pBandR[int(ii / float(rect.bottom - rect.top) * bands.iHeight)];
			UCHAR * TempG = bands.pBandG[int(ii / float(rect.bottom - rect.top) * bands.iHeight)];
			UCHAR * TempB = bands.pBandB[int(ii / float(rect.bottom - rect.top) * bands.iHeight)];
			LPBYTE pbLineData = pbData + ii * nPitch;
			for (int jj = 0; jj < int(float(rect.right - rect.left) / iNewWidth * bands.iWidth); jj++)
			{
				pbLineData[jj * 3] = TempB[int(jj / float(rect.right - rect.left) * iNewWidth)];	// B
				pbLineData[jj * 3 + 1] = TempG[int(jj / float(rect.right - rect.left) * iNewWidth)];	// G
				pbLineData[jj * 3 + 2] = TempR[int(jj / float(rect.right - rect.left) * iNewWidth)];	// R
			}
			for (int jj = int(float(rect.right - rect.left) / iNewWidth * bands.iWidth); jj < rect.right - rect.left; jj++)
			{
				pbLineData[jj * 3] = 255;	// B
				pbLineData[jj * 3 + 1] = 255;	// G
				pbLineData[jj * 3 + 2] = 255;	// R
			}
		}
	}

	for (int ii = 0; ii < rect.bottom - rect.top; ii++)
	{
		LPBYTE pbLineData = pbData + ii * nPitch;
		for (int jj = 0; jj < rect.right - rect.left; jj++)
		{
			if (ii == SelectArea.top || ii == SelectArea.bottom)
			{
				if (jj >= SelectArea.left && jj <= SelectArea.right)
				{
					pbLineData[jj * 3] = 0;	// B
					pbLineData[jj * 3 + 1] = 0;	// G
					pbLineData[jj * 3 + 2] = 255;	// R
				}
			}
			if (jj == SelectArea.left || jj == SelectArea.right)
			{
				if (ii >= SelectArea.top && ii <= SelectArea.bottom)
				{
					pbLineData[jj * 3] = 0;	// B
					pbLineData[jj * 3 + 1] = 0;	// G
					pbLineData[jj * 3 + 2] = 255;	// R
				}
			}
		}
	}
	img.Draw(pDC->m_hDC, rect);
}

void CViewportController::DrawControlPoint()
{
	CDC * pmainDC = NULL;
	CDC * pzoomDC = NULL;
	CDC * pscrollDC = NULL;
	if (m_ABFlagIsA == 0)
	{
		pmainDC = mainDlg->GetDC();
		pzoomDC = zoomDlg->GetDC();
		pscrollDC = scrollDlg->GetDC();
	}
	else if (m_ABFlagIsA == 1)
	{
		pmainDC = mainDlgA->GetDC();
		pzoomDC = zoomDlgA->GetDC();
		pscrollDC = scrollDlgA->GetDC();
	}
	for (int ii = 0; ii < CBandList::GetInstance().ConPoint->m_ControlPointList.size(); ii++)
	{
		if (m_ABFlagIsA == 0)
		{
			DrawMainControlPoint(pmainDC, CBandList::GetInstance().ConPoint->m_ControlPointList[ii].BasePoint);
			DrawZoomControlPoint(pzoomDC, CBandList::GetInstance().ConPoint->m_ControlPointList[ii].BasePoint);
			DrawScrollControlPoint(pscrollDC, CBandList::GetInstance().ConPoint->m_ControlPointList[ii].BasePoint);
		}
		if (m_ABFlagIsA == 1)
		{
			DrawMainControlPoint(pmainDC, CBandList::GetInstance().ConPoint->m_ControlPointList[ii].WarpPoint);
			DrawZoomControlPoint(pzoomDC, CBandList::GetInstance().ConPoint->m_ControlPointList[ii].WarpPoint);
			DrawScrollControlPoint(pscrollDC, CBandList::GetInstance().ConPoint->m_ControlPointList[ii].WarpPoint);
		}
	}
}

void CViewportController::DrawMainControlPoint(CDC *pDC, CPoint point)
{
	int iNewX = point.x - maindisplaypos.x;
	int iNewY = point.y - maindisplaypos.y;

	CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
	CPen *pOldPen = pDC->SelectObject(&pen);

	pDC->MoveTo(iNewX - 10, iNewY);
	pDC->LineTo(iNewX + 10, iNewY);
	pDC->MoveTo(iNewX, iNewY - 10);
	pDC->LineTo(iNewX, iNewY + 10);
}

void CViewportController::DrawZoomControlPoint(CDC * pDC, CPoint point)
{
	int iNewX = 3 * (point.x - zoomdisplaypos.x);
	int iNewY = 3 * (point.y - zoomdisplaypos.y);

	CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
	CPen *pOldPen = pDC->SelectObject(&pen);

	pDC->MoveTo(iNewX - 10, iNewY);
	pDC->LineTo(iNewX + 10, iNewY);
	pDC->MoveTo(iNewX, iNewY - 10);
	pDC->LineTo(iNewX, iNewY + 10);
}

void CViewportController::DrawScrollControlPoint(CDC * pDC, CPoint point)
{
	int iNewX = int((float)point.x / iNewWidth * (scrollrect.right - scrollrect.left));
	int iNewY = int((float)point.y / iNewHeight * (scrollrect.bottom - scrollrect.top));

	CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
	CPen *pOldPen = pDC->SelectObject(&pen);

	pDC->MoveTo(iNewX - 3, iNewY);
	pDC->LineTo(iNewX + 3, iNewY);
	pDC->MoveTo(iNewX, iNewY - 3);
	pDC->LineTo(iNewX, iNewY + 3);
}

BYTE CViewportController::AdjustData(BYTE bOriData, BYTE bmin, BYTE bmax)
{
	BYTE ReByte = bOriData;
	if (bOriData > bmax)
		ReByte = 255;
	else if (bOriData < bmin)
		ReByte = 0;
	else
	{
		ReByte = BYTE((bOriData - bmin) / (float)(bmax - bmin) * 255);
	}
	return ReByte;
}

void CViewportController::MainToZoom(CPoint point)
{
	if (point.x >= int(float(zoomrect.right - zoomrect.left) / 3) / 2 && point.x <= mainrect.right - mainrect.left - int(float(zoomrect.right - zoomrect.left) / 3) / 2)
	{
		mainpos.x = point.x;
	}
	else if (point.x < int(float(zoomrect.right - zoomrect.left) / 3) / 2)
	{
		mainpos.x = int(float(zoomrect.right - zoomrect.left) / 3) / 2;
	}
	else if (point.x > mainrect.right - mainrect.left - int(float(zoomrect.right - zoomrect.left) / 3) / 2)
	{
		mainpos.x = mainrect.right - mainrect.left - int(float(zoomrect.right - zoomrect.left) / 3) / 2;
	}

	if (point.y >= int(float(zoomrect.bottom - zoomrect.top) / 3) / 2 && point.y <= mainrect.bottom - mainrect.top - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2)
	{
		mainpos.y = point.y;
	}
	else if (point.y < int(float(zoomrect.bottom - zoomrect.top) / 3) / 2)
	{
		mainpos.y = int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;
	}
	else if (point.y > mainrect.bottom - mainrect.top - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2)
	{
		mainpos.y = mainrect.bottom - mainrect.top - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;

	}

	zoomdisplaypos.x = maindisplaypos.x + mainpos.x - int(float(zoomrect.right - zoomrect.left) / 3) / 2;
	zoomdisplaypos.y = maindisplaypos.y + mainpos.y - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;

	if (zoomdisplaypos.x < 0)
		zoomdisplaypos.x = 0;
	if (zoomdisplaypos.x > BaseBands.iWidth - (zoomrect.right - zoomrect.left) / 3 - 1)//1188
		zoomdisplaypos.x = BaseBands.iWidth - (zoomrect.right - zoomrect.left) / 3 - 1;
	if (zoomdisplaypos.y < 0)
		zoomdisplaypos.y = 0;
	if (zoomdisplaypos.y > BaseBands.iHeight - (zoomrect.bottom - zoomrect.top) / 3 - 1)//943
		zoomdisplaypos.y = BaseBands.iHeight - (zoomrect.bottom - zoomrect.top) / 3 - 1;


	DrawBaseImage();
	if(CBandList::GetInstance().ConPoint != NULL)
		DrawControlPoint();
}

void CViewportController::ScrollToMainAndZoom(CPoint point)
{
	int xmin;
	int xmax;

	int ymin;
	int ymax;

	if (BaseBands.iHeight > BaseBands.iWidth)
	{
		xmin = int(float(mainrect.right - mainrect.left) / iNewWidth * (scrollrect.right - scrollrect.left)) / 2;
		xmax = int(float(scrollrect.right - scrollrect.left) / iNewWidth * BaseBands.iWidth) - xmin;

		ymin = int(float(mainrect.bottom - mainrect.top) / iNewHeight * (scrollrect.bottom - scrollrect.top)) / 2;
		ymax = int(scrollrect.bottom - scrollrect.top - ymin);
	}
	else
	{
		xmin = int(float(mainrect.right - mainrect.left) / iNewWidth * (scrollrect.right - scrollrect.left)) / 2;
		xmax = int(scrollrect.right - scrollrect.left - xmin);

		ymin = int(float(mainrect.bottom - mainrect.top) / iNewHeight * (scrollrect.bottom - scrollrect.top)) / 2;
		ymax = int(float(scrollrect.bottom - scrollrect.top) / iNewHeight * BaseBands.iHeight) - ymin;
	}

	scrollpos.x = point.x;
	if (point.x >= xmin &&
		point.x <= xmax)
	{
		scrollpos.x = point.x;
	}
	else if (point.x < xmin)
	{
		scrollpos.x = xmin;
	}
	else if (point.x > xmax)
	{
		scrollpos.x = xmax;
	}
	scrollpos.y = point.y;
	if (point.y >= ymin &&
		point.y <= ymax)
	{
		scrollpos.y = point.y;
	}
	else if (point.y < ymin)
	{
		scrollpos.y = ymin;
	}
	else if (point.y > ymax)
	{
		scrollpos.y = ymax;
	}

	maindisplaypos.x = int(float(scrollpos.x) / (scrollrect.right - scrollrect.left) * iNewWidth - int(float(mainrect.right - mainrect.left)) / 2);
	maindisplaypos.y = int(float(scrollpos.y) / (scrollrect.bottom - scrollrect.top) * iNewHeight - int(float(mainrect.bottom - mainrect.top)) / 2);

	if (maindisplaypos.x < 0)
		maindisplaypos.x = 0;
	if (maindisplaypos.x > BaseBands.iWidth - (mainrect.right - mainrect.left) - 1)
		maindisplaypos.x = BaseBands.iWidth - (mainrect.right - mainrect.left) - 1;
	if (maindisplaypos.y < 0)
		maindisplaypos.y = 0;
	if (maindisplaypos.y > BaseBands.iHeight - (mainrect.bottom - mainrect.top) - 1)
		maindisplaypos.y = BaseBands.iHeight - (mainrect.bottom - mainrect.top) - 1;

	zoomdisplaypos.x = maindisplaypos.x + mainpos.x - int(float((zoomrect.right - zoomrect.left) / (mainrect.right - mainrect.left) * (zoomrect.right - zoomrect.left)) / 2);
	zoomdisplaypos.y = maindisplaypos.y + mainpos.y - int(float((zoomrect.bottom - zoomrect.top) / (mainrect.bottom - mainrect.top) * (zoomrect.bottom - zoomrect.top)) / 2);


	if (zoomdisplaypos.x < 0)
		zoomdisplaypos.x = 0;
	if (zoomdisplaypos.x > BaseBands.iWidth - (zoomrect.right - zoomrect.left) / 3 - 1)//1188
		zoomdisplaypos.x = BaseBands.iWidth - (zoomrect.right - zoomrect.left) / 3 - 1;
	if (zoomdisplaypos.y < 0)
		zoomdisplaypos.y = 0;
	if (zoomdisplaypos.y > BaseBands.iHeight - (zoomrect.bottom - zoomrect.top) / 3 - 1)//943
		zoomdisplaypos.y = BaseBands.iHeight - (zoomrect.bottom - zoomrect.top) / 3 - 1;

	DrawBaseImage();
	if (CBandList::GetInstance().ConPoint != NULL)
		DrawControlPoint();
}

void CViewportController::ZoomChange(CPoint point)
{
	zoompos.x = point.x;
	zoompos.y = point.y;

	DrawBaseImage();
	if (CBandList::GetInstance().ConPoint != NULL)
		DrawControlPoint();
}

void CViewportController::OnMainDialogClose()
{
	Destroy();
}

void CViewportController::Destroy()
{
	if (m_ABFlagIsA)
	{
		if (mainDlgA) {
			mainDlgA->DestroyWindow();
			mainDlgA = nullptr;
		}
		if (zoomDlgA) {
			zoomDlgA->DestroyWindow();
			zoomDlgA = nullptr;
		}
		if (scrollDlgA) {
			scrollDlgA->DestroyWindow();
			scrollDlgA = nullptr;
		}
		CBandList::GetInstance().ViewportAdjust = nullptr;
	}
	else
	{
		if (mainDlg) {
			mainDlg->DestroyWindow();
			mainDlg = nullptr;
		}
		if (zoomDlg) {
			zoomDlg->DestroyWindow();
			zoomDlg = nullptr;
		}
		if (scrollDlg) {
			scrollDlg->DestroyWindow();
			scrollDlg = nullptr;
		}
		CBandList::GetInstance().ViewportBase = nullptr;
	}
	delete this;
}

void CViewportController::GetMousePos()
{
	MousePos.x = zoomdisplaypos.x + zoompos.x / 3;
	MousePos.y = zoomdisplaypos.y + zoompos.y / 3;
}

void CViewportController::PredictPoint(CPoint point)
{
	int iNowX = int(float(point.x) / iNewWidth * (scrollrect.right - scrollrect.left));
	int iNowY = int(float(point.y) / iNewHeight * (scrollrect.bottom - scrollrect.top));

	int xmin = int(float(mainrect.right - mainrect.left) / iNewWidth * (scrollrect.right - scrollrect.left)) / 2;
	int xmax = int(scrollrect.right - scrollrect.left - xmin);

	int ymin = int(float(mainrect.bottom - mainrect.top) / iNewHeight * (scrollrect.bottom - scrollrect.top)) / 2;
	int ymax = int(float(scrollrect.bottom - scrollrect.top) / iNewHeight * BaseBands.iHeight) - ymin;

	scrollpos.x = iNowX;
	if (iNowX >= xmin &&
		iNowX <= xmax)
	{
		scrollpos.x = iNowX;
	}
	else if (iNowX < xmin)
	{
		scrollpos.x = xmin;
	}
	else if (iNowX > xmax)
	{
		scrollpos.x = xmax;
	}
	scrollpos.y = iNowY;
	if (iNowY >= ymin &&
		iNowY <= ymax)
	{
		scrollpos.y = iNowY;
	}
	else if (iNowY < ymin)
	{
		scrollpos.y = ymin;
	}
	else if (iNowY > ymax)
	{
		scrollpos.y = ymax;
	}

	maindisplaypos.x = int(float(scrollpos.x) / (scrollrect.right - scrollrect.left) * iNewWidth - int(float(mainrect.right - mainrect.left)) / 2);
	maindisplaypos.y = int(float(scrollpos.y) / (scrollrect.bottom - scrollrect.top) * iNewHeight - int(float(mainrect.bottom - mainrect.top)) / 2);

	if (maindisplaypos.x < 0)
		maindisplaypos.x = 0;
	if (maindisplaypos.x > BaseBands.iWidth - (mainrect.right - mainrect.left) - 1)
		maindisplaypos.x = BaseBands.iWidth - (mainrect.right - mainrect.left) - 1;
	if (maindisplaypos.y < 0)
		maindisplaypos.y = 0;
	if (maindisplaypos.y > BaseBands.iHeight - (mainrect.bottom - mainrect.top) - 1)
		maindisplaypos.y = BaseBands.iHeight - (mainrect.bottom - mainrect.top) - 1;

	point.x = point.x - maindisplaypos.x;
	point.y = point.y - maindisplaypos.y;

	if (point.x >= int(float(zoomrect.right - zoomrect.left) / 3) / 2 && point.x <= mainrect.right - mainrect.left - int(float(zoomrect.right - zoomrect.left) / 3) / 2)
	{
		mainpos.x = point.x;
	}
	else if (point.x < int(float(zoomrect.right - zoomrect.left) / 3) / 2)
	{
		mainpos.x = int(float(zoomrect.right - zoomrect.left) / 3) / 2;
	}
	else if (point.x > mainrect.right - mainrect.left - int(float(zoomrect.right - zoomrect.left) / 3) / 2)
	{
		mainpos.x = mainrect.right - mainrect.left - int(float(zoomrect.right - zoomrect.left) / 3) / 2;
	}

	if (point.y >= int(float(zoomrect.bottom - zoomrect.top) / 3) / 2 && point.y <= mainrect.bottom - mainrect.top - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2)
	{
		mainpos.y = point.y;
	}
	else if (point.y < int(float(zoomrect.bottom - zoomrect.top) / 3) / 2)
	{
		mainpos.y = int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;
	}
	else if (point.y > mainrect.bottom - mainrect.top - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2)
	{
		mainpos.y = mainrect.bottom - mainrect.top - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;

	}

	zoomdisplaypos.x = maindisplaypos.x + mainpos.x - int(float(zoomrect.right - zoomrect.left) / 3) / 2;
	zoomdisplaypos.y = maindisplaypos.y + mainpos.y - int(float(zoomrect.bottom - zoomrect.top) / 3) / 2;

	if (zoomdisplaypos.x < 0)
		zoomdisplaypos.x = 0;
	if (zoomdisplaypos.x > BaseBands.iWidth - (zoomrect.right - zoomrect.left) / 3 - 1)//1188
		zoomdisplaypos.x = BaseBands.iWidth - (zoomrect.right - zoomrect.left) / 3 - 1;
	if (zoomdisplaypos.y < 0)
		zoomdisplaypos.y = 0;
	if (zoomdisplaypos.y > BaseBands.iHeight - (zoomrect.bottom - zoomrect.top) / 3 - 1)//943
		zoomdisplaypos.y = BaseBands.iHeight - (zoomrect.bottom - zoomrect.top) / 3 - 1;

	point.x = point.x + maindisplaypos.x - zoomdisplaypos.x;
	point.y = point.y + maindisplaypos.y - zoomdisplaypos.y;

	zoompos.x = point.x * 3;
	zoompos.y = point.y * 3;

	DrawBaseImage();
	if (CBandList::GetInstance().ConPoint != NULL)
		DrawControlPoint();
}

CViewportController::~CViewportController()
{}
